﻿using DS_2024_02.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TYBase;
using MessageBox = System.Windows.MessageBox;

namespace EVEBatteryCheck.Views {
    /// <summary>
    /// ViewCfgSystem.xaml 的交互逻辑
    /// </summary>
    public partial class ViewCfgSystem : Window {
        public ViewCfgSystem() {
            InitializeComponent();
            this.Loaded += ViewCfgSystem_Loaded;
        }

        private void ViewCfgSystem_Loaded(object sender, RoutedEventArgs e) {
            this.txt_ccd_delay.Text = AppIniData.CCDDelaySecond.ToString();
            this.txt_robot_delay.Text = AppIniData.RobotDelaySecond.ToString();

            this.txt_ccd_outtime.Text = AppIniData.CCDWaitOuttime.ToString();
            this.txt_robot_outtime.Text = AppIniData.RobotWaitOuttime.ToString();
            this.txt_charge_dir.Text = AppIniData.Dir_PreCharge.ToString();
            this.txt_save_dir.Text = AppIniData.SaveDir.ToString();
        }

        private void BtnClick_OK(object sender, RoutedEventArgs e) {
            AppIniData.CCDDelaySecond = this.txt_ccd_delay.Text.AsInt();
            AppIniData.RobotDelaySecond = this.txt_robot_delay.Text.AsInt();

            AppIniData.CCDWaitOuttime = this.txt_ccd_outtime.Text.AsInt();
            AppIniData.RobotWaitOuttime = txt_robot_outtime.Text.AsInt();
            AppIniData.Dir_PreCharge = txt_charge_dir.Text;
            AppIniData.SaveDir = txt_save_dir.Text;


            AppData.ini.WriteInt(nameof(AppIniData.CCDDelaySecond), AppIniData.CCDDelaySecond);
            AppData.ini.WriteInt(nameof(AppIniData.RobotDelaySecond), AppIniData.RobotDelaySecond);

            AppData.ini.WriteInt(nameof(AppIniData.CCDWaitOuttime), AppIniData.CCDWaitOuttime);
            AppData.ini.WriteInt(nameof(AppIniData.RobotWaitOuttime), AppIniData.RobotWaitOuttime); 

            AppData.ini.WriteString(nameof(AppIniData.Dir_PreCharge), AppIniData.Dir_PreCharge);
            AppData.ini.WriteString(nameof(AppIniData.SaveDir), AppIniData.SaveDir);
            MessageBox.Show("生效!");

        }

        private void BtnClick_SelectDir(object sender, RoutedEventArgs e) {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog()) {
                dialog.Description = "请选择预充电软件目录";
                DialogResult result = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK) {
                    string selectedDirectory = dialog.SelectedPath;
                    txt_charge_dir.Text = selectedDirectory;
                }
            }
        }

        private void BtnClick_SelectSaveDir(object sender, RoutedEventArgs e) {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog()) {
                dialog.Description = "请选择文件保存目录";
                dialog.SelectedPath = txt_save_dir.Text;
                DialogResult result = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK) {
                    string selectedDirectory = dialog.SelectedPath;
                    txt_save_dir.Text = selectedDirectory;
                }
            }
        }
    }
}
